package apps.softtech.Utility;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

/**
 * 
 * @author MUI
 *
 */
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {

	private static final String TAG = "DownloadImage";
	ProgressDialog progress;
	Context mContext;
	ImageView imgAvatar;
	Bitmap mBitmap=null;
	
	public DownloadImage(Context context, ImageView img){
		mContext = context;
		imgAvatar= img;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progress = new ProgressDialog(mContext);
		progress.setMessage("Loading image ...");
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setIndeterminate(true);
		progress.show();
	}

	@Override
	protected Bitmap doInBackground(String... URL) {
		String imageURL = URL[0];
		Bitmap bitmap = null;
		try {
			// Download Image from URL
			InputStream input = new java.net.URL(imageURL).openStream();
			// Decode Bitmap
			bitmap = BitmapFactory.decodeStream(input);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mBitmap = bitmap;
		Log.e(TAG,"bitmap dobackround: " + mBitmap.toString());
		return bitmap;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		super.onPostExecute(result);
		mBitmap = result;
		Log.e(TAG,"bitmap result : " + result.toString());
//		result = ImageHelper.getRoundedCornerBitmap(result, 100);
		// Set the bitmap into ImageView
		imgAvatar.setImageBitmap(result);
		// Close progressdialog
		progress.dismiss();
	}

	public Bitmap getBitmap(){
		return this.mBitmap;
	}
}
